package com.panopset.java;

import java.io.File;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import com.panopset.RezIO;
import com.panopset.Strings;
import com.panopset.Util;
import com.panopset.UtilIO;

/**
 * Index source pre-compile time.
 *
 * <p>
 * For each source directory, create an index called index.lst.
 *
 * </p>
 * @author Karl Dinwiddie
 *
 */
public final class IndexSource {

    /**
     * Format Java source files.
     *
     * @param sourceFileDirectory
     *            Source directory to index.
     */
    public static void indexSource(final String sourceFileDirectory) {
        final File file = new File(sourceFileDirectory);
        Util.dspmsg("Indexing " + RezIO.getCanonicalPath(file));
        new IndexSource().index(file);
    }

    /**
     * IndexSource constructor.
     */
    private IndexSource() {
    }

    /**
     * args[n] = Java source code directories.
     *
     * @param args
     *            args[n] are the source root directories that you wish to
     *            index.
     */
    public static void main(final String[] args) {
        if (args == null) {
            return;
        }
        if (args.length < 1) {
            return;
        }
        for (int i = 0; i < args.length; i++) {
            File dir = new File(args[0]);
            if (!dir.exists()) {
                continue;
            }
            if (!dir.isDirectory()) {
                continue;
            }
            new IndexSource().index(dir);
        }
    }

    /**
     * Create indexes for each source directory, save it as index.lst
     * in the same source directory.
     *
     * @param src
     *            Source file.
     */
    private void index(final File src) {
        if (src.isDirectory()) {
            Set<File> set =
                Collections.synchronizedSet(new TreeSet<File>());
            for (File f : src.listFiles()) {
                index(f);
                if (f.isFile()) {
                    set.add(f);
                }
            }
            String srcPath = RezIO.getCanonicalPath(src);
            srcPath = srcPath.replace("\\", "/");
            srcPath = srcPath.replace(Strings.FILE_SEP, "/");
            StringWriter sw = new StringWriter();
            for (File f : set) {
                if (f.isFile()) {
                    String name = f.getName();
                    if (!"index.lst".equals(name)) {
                        if (".java".equals(UtilIO.getExtension(f))) {
                            name = name.replace(".java", ".class");
                        }
                        sw.append(name);
                        sw.append(Strings.getEol());
                    }
                }
            }
            File index = new File(srcPath + "/index.lst");
            if (sw.toString().length() > 0
                    && !sw.toString().equals("index.lst" + Strings.getEol())) {
                UtilIO.saveStringToFile(sw.toString(), index);
            } else {
                UtilIO.delete(index);
            }
        }
    }
}
